/*
 * 通过高德地图，获取小区位置信息。
*/
const axios = require('axios');
const names = require('../buyHouse/position');
const fs = require('fs');
const get = require('lodash').get;


let newNames = {};

let key = 'a140f1cb9404b3bfbda19682d40c21dc';
let keywords = '武圣西里';
let city = 010;
let types = 150500;
// let location = '116.465623,39.878689'
// let types = 190000;

function parseSubway(subways) {
    return Array.isArray(subways)
        ? subways.map(item => {
            return {
                name: item.name,
                adname: item.adname,
                address: item.address
            }
        })
        : [];
}


(async () => {
    let keys = Object.keys(names);
    for (let i =0, l = keys.length; i < l; i++) {
        let item = keys[i];
        let pois = await axios.get(`https://restapi.amap.com/v5/place/text?key=${key}&keywords=${encodeURIComponent(item)}&region=${city}`);
        let location = get(pois, 'data.pois[0].location', '');
        console.log(location);
        if (location) {
            await setTimeout(() => {}, 100);
            let subway = await axios.get(`https://restapi.amap.com/v5/place/around?key=${key}&radius=1000&region=${city}&types=${types}&location=${location}`);
            console.log(subway.data);
            newNames[item] = {
                subway: parseSubway(get(subway, 'data.pois', []))
            }
        }
    }
    let data = JSON.stringify(newNames);
    console.log(data);
    fs.writeFileSync('./posi.json', data);
})();




